package com.aau.edu.lock;

import java.util.concurrent.locks.ReentrantLock;

/**
 * 相比于Synchronized，ReentrantLock提供了更多的功能，例如：
 * 1、支持公平锁
 * 2、支持多条件变量
 * 3、可中断
 * 4、可设置超时时间
 * 与synchronized一样，都支持可重入锁
 */
public class ReentrantLockDemo01 {
    private static ReentrantLock lock = new ReentrantLock();
    public static void main(String[] args) {

        lock.lock();
        try {
            System.out.println("enter main");
            m1();
        }  finally {
            lock.unlock();
        }
    }

    public static void m1() {
        lock.lock();
        try {
            System.out.println("enter m1");
            m2();
        }  finally {
            lock.unlock();
        }
    }
    public static void m2() {
        lock.lock();
        try {
            System.out.println("enter m2");
            m3();
        }  finally {
            lock.unlock();
        }
    }
    public static void m3() {
        lock.lock();
        try {
            System.out.println("enter m3");
        }  finally {
            lock.unlock();
        }
    }

}
